package drops.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.table.DefaultTableModel;


public class CalendarPanel extends JPanel implements ActionListener, MouseListener, KeyListener {
	
	JPanel headerPanel;
	JPanel bodyPanel;
	JPanel monthPanel;
	
	JTable calendarTable;
	
	JButton prevButton, nextButton;
	
	JLabel[][] squares;
	JLabel monthHeader;

	Calendar today;
	
	Vector days;
	Vector numbers;
	
	DefaultTableModel tm;
	
	int month=0;
	int year=0;
		
	JScrollPane scrollPane;
	
	MainFrame mainFrame;
	MonthView monthView;
	
    public CalendarPanel(MainFrame mainFrame) {
    	this.mainFrame = mainFrame;
    	this.monthView = new MonthView();
 
		
		headerPanel = new JPanel();
		bodyPanel = new JPanel();
		
		prevButton = new JButton("<<");
		prevButton.addActionListener(this);
		nextButton = new JButton(">>");
		nextButton.addActionListener(this);
		
		days = new Vector();
		numbers = new Vector();
		
		today = Calendar.getInstance();
		month = today.get(Calendar.MONTH);
		year = today.get(Calendar.YEAR);
		
		
		monthHeader = new JLabel("<html><center><b>"+monthView.getMonth(month)+", "+year+"</b></center></html>");

		numbers = monthView.printMonth(month, year);

		headerPanel.setLayout(new BorderLayout());
		headerPanel.add(prevButton, BorderLayout.WEST);
		headerPanel.add(nextButton, BorderLayout.EAST);
		headerPanel.add(monthHeader, BorderLayout.CENTER);
		
		days.add("Sunday");
		days.add("Monday");
		days.add("Tuesday");
		days.add("Wednesday");
		days.add("Thursday");
		days.add("Friday");
		days.add("Saturday");	
		tm = new DefaultTableModel(numbers, days) {
			public boolean isCellEditable(int row, int col) {
				return false;
			}
		};
      	calendarTable = new JTable(tm);
      	calendarTable.doLayout();
      	calendarTable.setRowSelectionAllowed(false);
      	calendarTable.setGridColor(Color.blue);
		calendarTable.setFillsViewportHeight(false);
    	scrollPane = new JScrollPane(calendarTable);
    	calendarTable.setRowHeight(50);
		calendarTable.addMouseListener(this);
		bodyPanel.setLayout(new BorderLayout());
    	bodyPanel.add(scrollPane, BorderLayout.CENTER);
		
		setLayout(new BorderLayout());
		add(headerPanel, BorderLayout.NORTH);
		add(bodyPanel, BorderLayout.CENTER);  
	}

    public Date getSelectedDate() {
    	Object obj;
    	int day;
    	obj = tm.getValueAt(calendarTable.getSelectedRow(),calendarTable.getSelectedColumn());
    	day=Integer.parseInt(obj.toString());
    	today.set(year, month, day);
    	return today.getTime();
    }
    
    public void search() {
    	//TODO
    	mainFrame.transactionFrame();
    	mainFrame.searchTransaction(getSelectedDate(), "Date");
    	//pangkuha nung transactions
    }
    
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==prevButton){
			month = month-1;
			if(month<0){
				month=11;
				year=year-1;
			}
			monthHeader.setText("<html><center><b>"+monthView.getMonth(month)+", "+year+"</b></center></html>");
			numbers = monthView.printMonth(month, year);
			tm.setDataVector(numbers,days);
		}
		if(e.getSource()==nextButton){
			month = month+1;
			if(month>11){
				month=0;
				year=year+1;
			}
			monthHeader.setText("<html><center><b>"+monthView.getMonth(month)+", "+year+"</b></center></html>");
			numbers = monthView.printMonth(month, year);
			tm.setDataVector(numbers,days);
		}
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getClickCount()==2) {
			//kapag dinouble click
			search();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if (arg0.getKeyCode()==KeyEvent.VK_ENTER) {
			//kapag inenter
			search();
		}
	}
}
 